<template>
  <el-form :model="form" :rules="rules" ref="form" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名/手机" v-model="form.username" @focus="clearValidate('username')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname" @focus="clearValidate('nickname')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="clearValidate('password')"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPass">
      <el-input placeholder="确认密码" type="password" v-model="form.checkPass"></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPass: ""
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        nickname: [
          {
            required: true,
            message: "请输入你的名字",
            trigger: "blur"
          }
        ],
        captcha: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ],
        checkPass: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      // 判断是否已经输入了手机号
      if (!this.form.username || this.form.username.length !== 11) {
        this.$message({
          showClose: true,
          message: "请输入正确的手机号",
          type: "error"
        });
        return;
      }
      this.$axios({
        url: "/captchas",
        method: "post",
        data: {
          tel: this.form.username
        }
      }).then(res => {
        // console.log(res.data);
        this.$message.success("已成功发送验证码到您的手机");
      });
    },

    // 注册
    handleRegSubmit() {
      this.$refs.form.validate(isValid => {
        if (isValid) {
          // const registerData = {
          //   username: this.form.username,
          //   nickname: this.form.nickname,
          //   captcha: this.form.captcha,
          //   password: this.form.password
          // };
          const { checkPass, ...registerData } = this.form;
          // this.$axios({
          //   url: "/accounts/register",
          //   method: "post",
          //   data: registerData
          // }).then(res => {
          //   // console.log(res.data);
          //   this.$message.success("注册成功");
          //   this.$emit("logintab", 0);
          // });
          this.$store.dispatch("user/register", registerData).then(res => {
            this.$message.success("注册成功");
            // 注册成功后弹出登录框
            setTimeout(() => {
              this.$emit("logintab");
            }, 1000);
          });
        }
      });
    },

    clearValidate(prop) {
      this.$refs.form.clearValidate(prop);
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>